package com.qqtech.qquan.circle.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleMemberTypeDictEnum;
import com.qqtech.qquan.circle.enums.CircleOpenLevelDictEnum;
import com.qqtech.qquan.circle.model.CircleBase;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleMember;
import com.qqtech.qquan.circle.model.CircleMemberGradeVo;
import com.qqtech.qquan.circle.model.CircleMemberQuery;
import com.qqtech.qquan.circle.model.CircleMemberVo;
import com.qqtech.qquan.circle.service.CircleApplyService;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleMemberGradeService;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserFriendService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/circle/member")
public class CircleMemberController extends BaseWebController<CircleMember, CircleMemberQuery> {
	@Resource
	private CircleMemberService circleMemberService;
	@Resource
	private CircleMemberGradeService circleMemberGradeService;
    @Resource
    private CircleBaseService circleBaseService;
    @Resource
	private UserBaseService userBaseService;
    @Resource
    private CircleApplyService circleApplyService;
    @Resource
    private UserFriendService userFriendService;
	@Override
	protected BaseService<CircleMember> getBaseService() {
		return circleMemberService;
	}
	
	/**
	 * 我没有设置隐私的圈子列表
	 * @return
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/myUnPrivateCircles")
	@ResponseBody
	public Result myUnPrivateCircles() {
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		CircleMemberQuery memberQuery = new CircleMemberQuery();
		memberQuery.setMemberId(curUserId);
		memberQuery.setIsPrivate(YesNoEnum.NO.getCode());
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "circleId"));
		List<CircleMemberVo> memberList = circleMemberService.queryList(memberQuery, pageKit);
		List<CircleBaseVo> circleList =  Lists.newArrayList();
		if(memberList!=null && memberList.size()>0){
			List<Integer> circleIds = Lists.newArrayList();
			for(CircleMemberVo memberVo: memberList){
				circleIds.add(memberVo.getCircleId());
			}
			circleList = circleBaseService.queryListByCircleIds(circleIds);
		}
		result.put("circleList", circleList);
		return result;

	}
	
	/**
	 * 我设置隐私的圈子列表
	 * @return
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/myPrivateCircles")
	@ResponseBody
	public Result myPrivateCircles() {
		Result result = new Result();
		Integer curUserId = super.getUserId();//当前用户id
		CircleMemberQuery memberQuery = new CircleMemberQuery();
		memberQuery.setMemberId(curUserId);
		memberQuery.setIsPrivate(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "circleId"));
		List<CircleMemberVo> memberList = circleMemberService.queryList(memberQuery, pageKit);
		List<CircleBaseVo> circleList =  Lists.newArrayList();
		if(memberList!=null && memberList.size()>0){
			List<Integer> circleIds = Lists.newArrayList();
			for(CircleMemberVo memberVo: memberList){
				circleIds.add(memberVo.getCircleId());
			}
			circleList = circleBaseService.queryListByCircleIds(circleIds);
		}
		result.put("circleList", circleList);
		return result;

	}
	
	/**
	 * 详情-用于管理
	 * @param circleQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detail4manage")
	@ResponseBody
	public Result detail4manage(@RequestBody CircleMemberQuery memberQuery) {
		Result result = new Result();
		Integer id = memberQuery.getId();// 前端-参数
		if (id == null) {
			result.addErr("成员Id信息为空！");
			return result;
		}
		CircleMemberVo memberVo = circleMemberService.getById(id);
		if (memberVo == null) {
			result.addErr("找不到该成员！");
			return result;
		}
		UserBaseVo userVo = userBaseService.getById(memberVo.getMemberId());
		if(userVo!=null){
			memberVo.setMemberName(userVo.getNick());
			memberVo.setMemberImg(userVo.getLogo());
			memberVo.setSex(userVo.getSex());
		}
		
		if(memberVo.getGradeCode() != null){
			CircleMemberGradeVo memberGradeVo = circleMemberGradeService.getMemberGrade(memberVo.getCircleId(), memberVo.getGradeCode());
			if(memberGradeVo!=null){
				memberVo.setGradeName(memberGradeVo.getGradeName());
			}else{
				memberVo.setGradeName("");
			}
		}else{
			memberVo.setGradeName("");
		}
		
		result.put("member", memberVo);
		
		boolean isFriend = userFriendService.isFriend(userVo.getId(), super.getUserId());
		result.put("isFriend", isFriend);
		return result;
	}
	
	/**
	 * 查询圈子成员	
	 * @param circleId
	 * @return
	 */
	@RequestMapping(value = "/members4circle")
	@ResponseBody
	public Result members4circle(@RequestBody CircleMember circleMember){
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		if(circleId==null || circleId==0){
			result.addErr("圈子id为空！");
			return result;
		}
		CircleBase circleBase = circleBaseService.getById(circleId);
		if(circleBase==null){
			result.addErr("找不到该圈子！");
			return result;
		}
		Integer isShowMemberGrade = circleBase.getIsShowMemberGrade();
		boolean isShowMemberGradeB = isShowMemberGrade == YesNoEnum.YES.getCode() ? true:false;
		List<CircleMemberVo> members = circleMemberService.queryCircleFullMember(circleId);
		if(members!=null && members.size()>0){
			for(CircleMemberVo memberVo:members){
				if(!isShowMemberGradeB){
					memberVo.setGradeName("");
				}
			}
		}
		boolean canInviteUser = false;//是否可以邀请新成员
		boolean isGroupCreator = false;//是否群主
		boolean isGroupManager = false;//是否管理员
		if(!super.isVisitor()){
			CircleMemberVo curMember = circleMemberService.getCircleMember(super.getUserId(), circleId);
			if(curMember!=null){
				canInviteUser = true;
				if(curMember.isCircleCreator()){
					isGroupCreator = true;
				}else if(curMember.isCircleManager()){
					isGroupManager = true;
				}
			}else{
				canInviteUser = false;
				isGroupCreator = false;
			}
		}
		result.put("members", members);
		result.put("canInviteUser", canInviteUser);
		result.put("isGroupCreator", isGroupCreator);//用于判断是否群主
		result.put("isGroupManager", isGroupManager);//用于判断是否管理员
		return result;
	}
	
	/**
	 * 查询圈子成员
	 * 管理圈子的成员(专用)
	 * @param circleId
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/members4manageCircle")
	@ResponseBody
	public Result members4manageCircle(@RequestBody CircleMember circleMember){
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		CircleBase circleBase = circleBaseService.getById(circleId);
		if(circleBase==null){
			result.addErr("找不到该圈子！");
			return result;
		}
		List<CircleMemberVo> members = circleMemberService.queryCircleFullMember(circleId);
		result.put("members", members);
		return result;
	}
	
	/**
	 * 加入圈子
	 * @param circleMember
	 * @return
	 * @throws BizzException 
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/joinCircle")
	@ResponseBody
	public Result joinCircle(@RequestBody CircleMember circleMember) throws BizzException{
		Result result = new Result();
		if(circleMember==null){
			result.addErr("新增成员信息为空！");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		Integer circleId =circleMember.getCircleId();
		CircleBaseVo circleVo = circleBaseService.getById(circleId);
		if (circleVo.isPrivate()) {//私密圈子
			result.addErr("不能直接加入私有圈子！");
			return result;
		}else if(circleVo.getOpenLevel()== CircleOpenLevelDictEnum.HALF_OPEN.getCode()){
			//半开放圈子，提交加入申请
			int appId = circleApplyService.apply2JoinCircle(curUserId, circleId);
			if(appId>0){
				result.put("myMsg", "您已成功提交了加入申请");
			}
		}else if(circleVo.getOpenLevel()== CircleOpenLevelDictEnum.FULL_OPEN.getCode()){
			//全开放圈子，直接加入
			circleMemberService.handleJoinCircle(curUserId, circleId, true);//直接加入
		}
		return result;
	}
	
	/**
	 * 扫 圈子 二维码  加入圈子
	 * @param circleId
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/scan2joinCircle")
	@ResponseBody
	public  Result scan2joinCircle(@RequestBody Map<String, String> map){
		Result result = new Result();
		if(super.isVisitor()){
			result.addErr("你还没有登陆!");
			return result;
		}
		String bizzId = map.get("bizzId").toString();
		if(bizzId==null){
			result.addErr("圈子id为空!");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		Integer circleId = Integer.parseInt(bizzId);
		try {
			circleMemberService.handleJoinCircle(curUserId, circleId, true);
		} catch (BizzException e) {
			result.addErr(e.getMsg());
		}
		return result;
	}
	/**
	 * 用户主动退出圈子
	 * @param circleMember
	 * @return
	 * @throws BizzException 
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/quitCircle")
	@ResponseBody
	public Result quitCircle(@RequestBody CircleMember circleMember) throws BizzException{
		Result result = new Result();
		if(circleMember==null){
			result.addErr("圈子成员信息为空！");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		Integer circleId =circleMember.getCircleId();
		circleMemberService.handleQuitCircle(curUserId, circleId);
		return result;
	}
	
	/**
	 * 圈主/管理员  踢走某个成员
	 * @param circleMember
	 * @return
	 * @throws BizzException 
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/kickMember")
	@ResponseBody
	public Result kickMember(@RequestBody CircleMember circleMember) throws BizzException{
		Result result = new Result();
		if(circleMember==null){
			result.addErr("成员信息为空！");
			return result;
		}
		Integer circleId =circleMember.getCircleId();
		Integer memberId =circleMember.getMemberId();
		Integer curUserId = super.getUserId();//当前用户id
		CircleBaseVo circleVo = circleBaseService.getById(circleId);
		boolean isGroupChat = false;//是否群聊
		if(circleVo!=null){
			if(circleVo.isGroupChat()){
				isGroupChat = true;
			}
		}
		if(curUserId.intValue()==memberId.intValue()){
			result.addErr("您不能踢走自己！");
			return result;
		}
		if(circleBaseService.isCircleCreator(curUserId, circleId) || circleMemberService.isCircleManager(curUserId, circleId)){
			circleMemberService.handleKickMember(memberId, circleId);
		}else{
			if(isGroupChat){
				result.addErr("您不是群主或管理员！");
			}else{
				result.addErr("您不是圈主或管理员！");
			}
		}
		return result;
	}
	
	/**
	 * 添加对该圈子的隐私
	 * @param circleMember
	 * 			{circleId: xxx}
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/addPrivate")
	@ResponseBody
	public Result addPrivate(@RequestBody CircleMemberVo circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		circleMember.setMemberId(curUserId);
		CircleMemberVo memberVo = circleMemberService.getCircleMember(curUserId,circleId);
		if(memberVo==null){
			result.addErr("您不是该圈子的成员.");
			return result;
		}		
		CircleMember member4update = new CircleMember();
		member4update.setId(memberVo.getId());
		member4update.setIsPrivate(YesNoEnum.YES.getCode());
		circleMemberService.updateById(member4update);
		return result;
	}
	
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/addPrivateBath")
	@ResponseBody
	public Result addPrivateBath(@RequestBody Map<String, String> map) throws BizzException{
		Result result = new Result();
		String circleIds = map.get("circleIds").toString();
		Integer curUserId = super.getUserId();//当前用户id
		if(StringUtil.isBlank(circleIds)){
			result.addErr("您没有选择圈子!");
			return result;
		}
		String[] circleIdsArray = circleIds.split(";");
		CircleMemberVo memberDb = null;
		CircleMember member4update = null;
		for(String circleId : circleIdsArray){
			memberDb = circleMemberService.getCircleMember(curUserId,Integer.valueOf(circleId));
			if(memberDb!=null){
				member4update = new CircleMember();
				member4update.setId(memberDb.getId());
				member4update.setIsPrivate(YesNoEnum.YES.getCode());
				circleMemberService.updateById(member4update);
			}		
		}
		return result;
	}
	
	/**
	 * 删除对该圈子的隐私
	 * @param circleMember
	 * 			{circleId: xxx}
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/delPrivate")
	@ResponseBody
	public Result delPrivate(@RequestBody CircleMemberVo circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		circleMember.setMemberId(curUserId);
		CircleMemberVo memberVo = circleMemberService.getCircleMember(curUserId, circleId);
		if(memberVo==null){
			result.addErr("您不是该圈子的成员.");
			return result;
		}		
		CircleMember member4update = new CircleMember();
		member4update.setId(memberVo.getId());
		member4update.setIsPrivate(YesNoEnum.NO.getCode());
		circleMemberService.updateById(member4update);
		return result;
	}
	
	/**
	 * 添加圈子管理员
	 * 从成员列表中选择成员
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/setCircleManager")
	@ResponseBody
	public Result setCircleManager(@RequestBody CircleMemberVo circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		Integer memberId = circleMember.getMemberId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		if(!circleBaseService.isCircleCreator(curUserId, circleId)){
			result.addErr("您不是圈主，无权限操作.");
			return result;
		}
		circleMemberService.addCircleManager(memberId, circleId);
		return result;
	}
	
	/**
	 * 取消圈子管理员
	 * 从成员列表中选择成员
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/cancleCircleManager")
	@ResponseBody
	public Result cancleCircleManager(@RequestBody CircleMemberVo circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		Integer memberId = circleMember.getMemberId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		CircleBaseVo circleVo = circleBaseService.getById(circleId);
		boolean isGroupChat = false;//是否群聊
		if(circleVo!=null){
			if(circleVo.isGroupChat()){
				isGroupChat = true;
			}
		}
		Integer curUserId = super.getUserId();//当前用户id
		if(!circleBaseService.isCircleCreator(curUserId, circleId)){
			if(isGroupChat){
				result.addErr("您不是群主，无权限操作.");
			}else{
				result.addErr("您不是圈主，无权限操作.");
			}
			return result;
		}
		circleMemberService.removeCircleManager(memberId, circleId);
		return result;
	}
	
	/**
	 * 屏蔽(拉黑)成员
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/setBlack")
	@ResponseBody
	public Result setBlack(@RequestBody CircleMember circleMember) throws BizzException{
		Result result = new Result();
		if(circleMember==null){
			result.addErr("成员信息为空！");
			return result;
		}
		Integer circleId =circleMember.getCircleId();
		Integer memberId =circleMember.getMemberId();
		Integer curUserId = super.getUserId();//当前用户id
		if(curUserId.intValue()==memberId.intValue()){
			result.addErr("您不能屏蔽自己！");
			return result;
		}
		if(circleBaseService.isCircleCreator(curUserId, circleId) || circleMemberService.isCircleManager(curUserId, circleId)){
			circleMemberService.setBlack(memberId, circleId,true);
		}else{
			result.addErr("您不是圈主或管理员！");
			return result;
		}
		
		return result;
	}
	
	/**
	 * 取消屏蔽(拉黑)成员
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/unSetBlack")
	@ResponseBody
	public Result unSetBlack(@RequestBody CircleMember circleMember) throws BizzException{
		Result result = new Result();
		if(circleMember==null){
			result.addErr("圈子成员信息为空！");
			return result;
		}
		Integer circleId =circleMember.getCircleId();
		Integer memberId =circleMember.getMemberId();
		Integer curUserId = super.getUserId();//当前用户id
		if(curUserId.intValue()==memberId.intValue()){
			result.addErr("您不能操作自己！");
			return result;
		}
		if(circleBaseService.isCircleCreator(curUserId, circleId) || circleMemberService.isCircleManager(curUserId, circleId)){
			circleMemberService.setBlack(memberId, circleId,false);
		}else{
			result.addErr("您不是圈主或管理员！");
			return result;
		}
		
		return result;
	}
	
	/**
	 * 授权成员发布-投票
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/authAddVote")
	@ResponseBody
	public Result authAddVote(@RequestBody CircleMemberVo circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		Integer memberId = circleMember.getMemberId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		if(memberId==null){
			result.addErr("成员id为空.");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		if(!circleBaseService.isCircleCreator(curUserId, circleId)){
			result.addErr("您不是圈主,不能授权.");
			return result;
		}
		circleMemberService.handleCanAddVote(circleId, memberId, YesNoEnum.YES);
		return result;
	}
	
	/**
	 * 撤消授权成员发布-投票
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/unAuthAddVote")
	@ResponseBody
	public Result unAuthAddVote(@RequestBody CircleMemberVo circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		Integer memberId = circleMember.getMemberId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		if(memberId==null){
			result.addErr("成员id为空.");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		if(!circleBaseService.isCircleCreator(curUserId, circleId)){
			result.addErr("您不是圈主,不能操作.");
			return result;
		}
		circleMemberService.handleCanAddVote(circleId, memberId, YesNoEnum.NO);
		return result;
	}
	
	/**
	 * 授权成员发布-活动
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/authAddActivity")
	@ResponseBody
	public Result authAddActivity(@RequestBody CircleMemberVo circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		Integer memberId = circleMember.getMemberId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		if(memberId==null){
			result.addErr("成员id为空.");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		if(!circleBaseService.isCircleCreator(curUserId, circleId)){
			result.addErr("您不是圈主,不能授权.");
			return result;
		}
		circleMemberService.handleCanAddActivity(circleId, memberId, YesNoEnum.YES);
		return result;
	}
	
	/**
	 * 撤消授权成员发布-活动
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/unAuthAddActivity")
	@ResponseBody
	public Result unAuthAddActivity(@RequestBody CircleMemberVo circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		Integer memberId = circleMember.getMemberId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		if(memberId==null){
			result.addErr("成员id为空.");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		if(!circleBaseService.isCircleCreator(curUserId, circleId)){
			result.addErr("您不是圈主,不能操作.");
			return result;
		}
		circleMemberService.handleCanAddActivity(circleId, memberId, YesNoEnum.NO);
		return result;
	}
	
	/**
	 * 拉人进入群聊（直接加入）
	 * @param map
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/pull2joinCircle")
	@ResponseBody
	public Result pull2joinCircle(@RequestBody Map<String, String> map) throws BizzException{
		Result result = new Result();
		String beInviterIdsString = map.get("beInviterIds").toString();
		String circleIdStr = map.get("circleId").toString();
		
		if(StringUtil.isBlank(circleIdStr)){
			result.addErr("群聊 id为空");
			return result;
		}
		if(StringUtil.isBlank(beInviterIdsString)){
			result.addErr("成员信息为空");
			return result;
		}
		Integer circleId = Integer.parseInt(circleIdStr);
		String[] beInviterIdsArray = beInviterIdsString.split(";");
		for(String bId : beInviterIdsArray){
			circleMemberService.handleJoinGroupChat(Integer.parseInt(bId), circleId);
		}
		return result;
	}
	
	/**
	 * 设置成员等级头衔
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/setGrade")
	@ResponseBody
	public Result setGrade(@RequestBody CircleMember circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		Integer memberId = circleMember.getMemberId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		if(memberId==null){
			result.addErr("成员id为空.");
			return result;
		}
		if(circleMember.getGradeCode()==null){
			result.addErr("等级代码为空.");
			return result;
		}
		Integer curUserId = super.getUserId();//当前用户id
		if(!circleBaseService.isCircleCreator(curUserId, circleId) && !circleMemberService.isCircleManager(curUserId, circleId)){
			result.addErr("您不是圈主或管理员");
			return result;
		}
		
		CircleMemberQuery memberQ = new CircleMemberQuery();//用于查询这个成员
		memberQ.setCircleId(circleId);
		memberQ.setMemberId(memberId);
		
		CircleMemberVo memberVo = circleMemberService.getCircleMember(memberId, circleId);
		if(memberVo==null){
			result.addErr("找不到该成员.");
			return result;
		}		
		CircleMember member4update = new CircleMember();
		member4update.setId(memberVo.getId());
		member4update.setGradeCode(circleMember.getGradeCode());
		circleMemberService.updateById(member4update);
		return result;
	}
	
	/**
	 * 删除成员的等级头衔
	 * @param circleMember
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type=RespEnum.LOGIN)
	@RequestMapping(value = "/removeGrade")
	@ResponseBody
	public Result removeGrade(@RequestBody CircleMember circleMember) throws BizzException{
		Result result = new Result();
		Integer circleId = circleMember.getCircleId();
		Integer memberId = circleMember.getMemberId();
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		if(memberId==null){
			result.addErr("成员id为空.");
			return result;
		}
		
		Integer curUserId = super.getUserId();//当前用户id
		if(!circleBaseService.isCircleCreator(curUserId, circleId) && !circleMemberService.isCircleManager(curUserId, circleId)){
			result.addErr("您不是圈主或管理员");
			return result;
		}
		
		CircleMemberVo memberVo = circleMemberService.getCircleMember(memberId, circleId);
		if(memberVo==null){
			result.addErr("找不到该成员.");
			return result;
		}		
		CircleMember member4update = new CircleMember();
		member4update.setId(memberVo.getId());
		member4update.setGradeCode(0);//去除
		circleMemberService.updateById(member4update);
		return result;
	}
	
	/**
	 * 设置成员等级头衔-批量
	 * @param map
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/setGradeBath")
	@ResponseBody
	public Result setGradeBath(@RequestBody Map<String, String> map) throws BizzException {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id(圈子创建人)
		
		Integer circleId = Integer.valueOf(map.get("circleId"));
		Integer gradeCode = Integer.valueOf(map.get("gradeCode"));
		String memberIdsString = map.get("memberIds");
		String[] memberIdsArray = memberIdsString.split(";");
		List<Integer> memberIds = Lists.newArrayList();
		for (int i = 0; i < memberIdsArray.length; i++) {
			if (StringUtil.isNotBlank(memberIdsArray[i]) && curUserId != Integer.parseInt(memberIdsArray[i])) {
				memberIds.add(Integer.parseInt(memberIdsArray[i]));
			}
		}
		if(memberIds==null || memberIds.size()==0){
			result.addErr("没有选择成员");
			return result;
		}

		if(circleId==null){
			result.addErr("圈子id为空");
			return result;
		}
		
		if(gradeCode==null){
			result.addErr("等级代码为空");
			return result;
		}
		if(!circleBaseService.isCircleCreator(curUserId, circleId) && !circleMemberService.isCircleManager(curUserId, circleId)){
			result.addErr("您不是圈主或管理员");
			return result;
		}
		
		CircleMember circleMember = new CircleMember();
		circleMember.setCircleId(circleId);
		CircleMember member4update = null;
		for(Integer memberId : memberIds){
			circleMember.setMemberId(memberId);
			CircleMemberVo memberVo = circleMemberService.getCircleMember(memberId, circleId);
			if(memberVo==null){
				result.addErr("找不到该成员.");
				return result;
			}		
			member4update = new CircleMember();
			member4update.setId(memberVo.getId());
			member4update.setGradeCode(gradeCode);
			circleMemberService.updateById(member4update);
		}
		
		return result;
	}
	
	/**
	 * 查询一个圈子里同一个等级代码的成员
	 * @param circleMember
	 * @return
	 */
	@RequestMapping(value = "/members4Grade")
	@ResponseBody
	public Result members4Grade(@RequestBody CircleMemberQuery memberQuery) {
		Result result = new Result();
		if(memberQuery.getCircleId()==null){
			result.addErr("圈子id为空");
			return result;
		}
		if(memberQuery.getGradeCode()==null){
			result.addErr("等级代码为空");
			return result;
		}
		
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "circleId"));
		List<CircleMemberVo> memberList = circleMemberService.queryList(memberQuery, pageKit);
		if(memberList!=null && memberList.size()>0){
			UserBaseVo userVo = null;
			for(CircleMemberVo memberVo:memberList){
				userVo  = userBaseService.getById(memberVo.getMemberId());
				if(userVo!=null){
					memberVo.setMemberName(userVo.getNick());
					memberVo.setMemberImg(userVo.getLogo());
				}				
			}
		}
		result.put("memberList", memberList);
		return result;

	}
	
	/**
	 * 查询一个圈子里没有等级代码的成员（不含圈主及管理员）
	 * @param circleMember
	 * @return
	 */
	@RequestMapping(value = "/members4NoGrade")
	@ResponseBody
	public Result members4NoGrade(@RequestBody CircleMemberQuery memberQuery) {
		Result result = new Result();
		if(memberQuery.getCircleId()==null){
			result.addErr("圈子id为空");
			return result;
		}
		
		memberQuery.setType(CircleMemberTypeDictEnum.MEMBER.getCode());
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "memberId"));
		List<CircleMemberVo> memberListAll = circleMemberService.queryList(memberQuery, pageKit);
		List<CircleMemberVo> memberList = Lists.newArrayList();
		for(CircleMemberVo member :memberListAll){
			UserBaseVo userVo = null;
			if(member.getGradeCode()==null || member.getGradeCode()==0){
				userVo  = userBaseService.getById(member.getMemberId());
				if(userVo!=null){
					member.setMemberName(userVo.getNick());
					member.setMemberImg(userVo.getLogo());
				}
				memberList.add(member);
			}
		}
		result.put("memberList", memberList);
		return result;

	}
	
	/**
	 * 判断是否圈子成员
	 * @param member
	 * @return
	 * @throws BizzException
	 */
	@RequestMapping(value = "/isCircleMember")
	@ResponseBody
	public Result isCircleMember(@RequestBody CircleMember member) throws BizzException{
		Result result = new Result();
		Integer circleId = member.getCircleId();
		Integer memberId = member.getMemberId();
		
		if(circleId==null){
			result.addErr("圈子id为空.");
			return result;
		}
		if(memberId==null){
			result.addErr("成员id为空.");
			return result;
		}
		if(circleMemberService.isCircleMember(memberId, circleId)){
			result.put("isMember", true);
		}else{
			result.put("isMember", false);
		}
		return result;
	}
}
